<?php
/**
 * Created by PhpStorm.
 * User: anne
 * Date: 15/5/4
 * Time: 下午4:36
 */
namespace Common\Util;


class LibUtil {

    /**
     * 判断是否是微信内打开
     * @return bool
     */
    public static function isWeixin(){
        if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
            return true;
        }
        return false;
    }
    /**
     * 分页格式
     * @param $data
     * @return array
     */
    public static function pageFomate ($data) {
        $data = is_object($data) ? $data->all()  : $data;

        return [
            "total" => $data['total'],
            "per_page" => $data['per_page'],
            "current_page" => $data['current_page'],
            "last_page" => $data['last_page'],
            'list'=>$data['data']
        ];
    }



    /**
     * 得到图片地址
     * @param $imageId
     * @param int $kind
     * @param string $dir
     * @return string
     */

    public  static function getPicUrl($imageId,$kind = 4,$dir=''){

        if ($imageId == '' || $imageId == null) {
            return '';
        }
        if (empty($dir)) {
            $dir = 'uploads/images/';
        }
        $path = LibUtil::getFacePath($imageId);//中间地址段
//        $base =  $path. '/';
        $pic = $imageId.LibUtil::getPicName($kind).'.jpg' ;//得到name，id+大小标识+jpg后缀
        //检查图片是否存在，不存在返回空
        $basepath =$dir.$path;
        $url = $_SERVER["SERVER_NAME"].'/'.$basepath.$pic;


            return $url;

    }

    /**
     * 创建文件夹目录
     * @param string $folder   指定路径
     * @return bool
     */
    public static function make_dir($folder) {
        $reval = false;

        if (! file_exists ( $folder )) {
            /* 如果目录不存在则尝试创建该目录 */
            @umask ( 0 );

            /* 将目录路径拆分成数组 */
            preg_match_all ( '/([^\/]*)\/?/i', $folder, $atmp );

            /* 如果第一个字符为/则当作物理路径处理 */
            $base = ($atmp [0] [0] == '/') ? '/' : '';

            /* 遍历包含路径信息的数组 */
            foreach ( $atmp [1] as $val ) {
                if ('' != $val) {
                    $base .= $val;

                    if ('..' == $val || '.' == $val) {
                        /* 如果目录为.或者..则直接补/继续下一个循环 */
                        $base .= '/';

                        continue;
                    }
                } else {
                    continue;
                }

                $base .= '/';

                if (! file_exists ( $base )) {
                    /* 尝试创建目录，如果创建失败则继续循环 */
                    if (@mkdir ( rtrim ( $base, '/' ), 0777 )) {
                        @chmod ( $base, 0777 );
                        $reval = true;
                    }
                }
            }
        } else {
            /* 路径已经存在。返回该路径是不是一个目录 */
            $reval = is_dir ( $folder );
        }

        clearstatcache ();

        return $reval;
    }

    public static function fieldPicArr($file,$path) {
        $arr['file'] = $file;
        $arr['path'] = $path;
        $arr['arr'] = array(
            array('w' => 640, 'h' => 240, 'name' => '_b', 'isCut' => '1'),
            array('w' => 320, 'h' => 320, 'name' => '_m', 'isCut' => '1'),
            array('w' => 240, 'h' => 240, 'name' => '_s', 'isCut' => '1'),
        );
        return $arr;
    }

    /**
     *通过图片id，得到该图片中间地址段
     *@param $imageid
     * @return string $path
     */
    public static function getFacePath($imageid) {
        $key = "duododo.com$";
        $hash = md5($key."\t".$imageid."\t".strlen($imageid)."\t".$imageid % 10);
        $path = $hash{$imageid % 32} . "/" . abs(crc32($hash) % 100) . "/";

        return $path;
    }

    /**
     *通过图片id，得到该图片name的尾部标识（大小）
     *@param $kind
     * @return string
     */
    public static function getPicName($kind = 4) {
        $type = array(
            '_s','_m','_b','_o'
        );
        return isset($type[$kind]) ? $type[$kind] : $type[3];
    }

    /**
     * 请求地址
     *
     * @param string $url		请求地址
     * @param array $queries	请求参数
     * @return mixed
     *
     * @return json
     */
    public static function curlPost($url, $queries)
    {
        $ch = curl_init(); // 初始化curl
        curl_setopt($ch, CURLOPT_URL, $url); // 抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0); // 设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1 ); // post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($queries));
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        $result = curl_exec($ch); //运行curl
        curl_close($ch);
        return json_decode($result, true);
    }

    public static function reqPost($url, $queries)
    {
        $ch = curl_init(); // 初始化curl
        curl_setopt($ch, CURLOPT_URL, $url); // 抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0); // 设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1 ); // post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($queries));
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        $result = curl_exec($ch); //运行curl
        return $result;
    }
    /**
     * 请求地址
     *
     * @param string $url		请求地址
     * @return mixed
     *
     * @return JSON
     */
    public static function curlGet($url){
        $ch = curl_init(); // 初始化curl
        curl_setopt($ch, CURLOPT_URL, $url); // 抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0); // 设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 要求结果为字符串且输出到屏幕上
        $result = curl_exec($ch); //运行curl
        curl_close($ch);
        return json_decode($result, true);
    }

    public static function getKeyValue($url) {
        $result = array();
        $mr = preg_match_all('/(\?|&)(.+?)=([^&?]*)/i', $url, $matchs);

        if ($mr !== FALSE) {
            for ($i = 0; $i < $mr; $i++) {
                $result[$matchs[2][$i]] = $matchs[3][$i];
            }
        }

        return $result;
    }
    /**
     * 版本检查
     * @param unknown $first
     * @param unknown $second
     * @return boolean
     */
    public static function isSecondBigger($first, $second) {
        //纯数字比较
        if (is_numeric ( $first ) && is_numeric ( $second )) {
            if ($second > $first) {
                return true;
            } else {
                return false;
            }
        }
        $first = str_replace('.','',$first);
        $second = str_replace('.','',$second);
        $f =  strlen($first);
        $s =  strlen($second);

        if ($f>$s) {
            $n = $f-$s;
            $m = 1;
            for ($i=0;$i<$n;$i++){
                $m = $m*10;
            }
            $second = $second * $m;
        }else if($s>$f) {
            $n = $s-$f;
            $m = 1;
            for ($i=0;$i<$n;$i++){
                $m = $m*10;
            }
            $first = $first * $m;
        }
        if (intval($second) > intval($first)) {
            return true;
        }

        return false;
    }

    /**
     *
     * Expressions calculation function
     * @param String $expression
     * @param mixed $varibles
     * @return
     */

//    public function is_callable($args)
//    {
//    return is_callable($args);
//    }


    public static function parseExpression( $expression, $varibles = array() )
    {
        if ( false === preg_match_all( '/(\w+)/', $expression, $result, PREG_PATTERN_ORDER ) )
        {
            trigger_error( "expression is not correct!", E_USER_ERROR );
            return false;
        }
        $keys = $result[0];
        if ( ! isset( $varibles['true'] ) )
            $varibles['true'] = 1;
        if ( ! isset( $varibles['false'] ) )
            $varibles['false'] = 0;
        $varArray = array();
        $pos = 0;
        foreach ( $keys as $value )
        {
            if ( ( is_numeric( $value ) ) || (is_callable( $value ) ) )
            {
                continue;
            }
            if ( isset( $varibles[$value] ) )
            {
                $pos = strpos( $expression, $value, $pos );
                $expression = substr_replace( $expression, '$', $pos, 0 );
                $pos += strlen( $value ) + 1;
                $varArray[$value] = $varibles[$value];
            }
        }
        $cal = create_function( '$varArray', 'extract($varArray); return ' . "$expression" .';' );
        return $cal( $varArray );
    }


    public function monitoringTime($function){
        $starttime = explode(' ',microtime());
//        echo microtime();
        /*········以下是代码区·········*/
        $function;
        /*········以上是代码区·········*/
        //程序运行时间
        $endtime = explode(' ',microtime());
        $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
        $thistime = round($thistime,3);


        $res= "本网页执行耗时：".$thistime." 秒。".time();
        return $res;
    }

}